package com.xjtu.platform.reader.controller;

import com.xjtu.platform.common.*;
import com.xjtu.platform.reader.*;
import com.xjtu.platform.reader.dto.*;
import com.xjtu.platform.reader.entity.BookListEntity;
import com.xjtu.platform.reader.newEntity.LabelAndClass;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.*;

@RestController
@RequestMapping("/reader")
public class ReaderController {
    @Resource
    private ReaderService readerService;

    @Resource
    private BorrowService borrowService;

    @Resource
    private CommonService commonService;

    @Resource
    private AsyncService asyncService;

    @Resource
    private ReserveService reserveService;

    @Resource
    private NewService newService;

    @PostMapping("/get_booklist")
    public Result getBookList(@RequestBody GetBookListParamDto dto){
        try {
            Map<String, Object> page = readerService.getBookList(dto);
            System.out.println(page.toString());
            return Result.success("成功",page);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(Result.ERR_CODE_SYS, Constants.ERROR_MESSAGE);
        }
    }

    @PostMapping("/borrow_book")
    public Result borrowBook(@RequestBody BorrowBookDto borrowBookDto, @RequestHeader("Token") String token){
        String reader_id = TokenUtils.getUserInfo(token,commonService).getUserId();
        boolean isBorrowBook = readerService.borrowBook(borrowBookDto,reader_id);
        if(isBorrowBook){
            return Result.success("借书成功");
        }else {
            return Result.fail(Result.ERR_CODE_SYS,Constants.ERROR_MESSAGE);
        }
    }

    @PostMapping("/get_borrow_list")
    public Result getBorrowHis(@RequestBody BorrowHisDto borrowHisDto, @RequestHeader String token){
        try {
            String reader_id = TokenUtils.getUserInfo(token,commonService).getUserId();
            borrowHisDto.setReader_id(reader_id);
            Map<String, Object> page = readerService.getBorrowHis(borrowHisDto);
            return Result.success("成功",page);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(Result.ERR_CODE_SYS,Constants.ERROR_MESSAGE);
        }
    }

    @PostMapping("/return_book")
    public Result returnBook(@RequestBody ReturnBookDto returnBookDto){
        Date date = new Date();
        Timestamp end_time = new Timestamp(date.getTime());
        boolean isReturn = borrowService.returnBookToHis(returnBookDto,end_time);
        boolean isPenalty = borrowService.penaltyReader(returnBookDto.getBook_reader_his_id(),end_time);
        if(isReturn){
            if(isPenalty){
                asyncService.handleReserveAfterReturn(returnBookDto.getBook_id());
                return Result.success("还书成功");
            }else {
                return Result.fail(Result.ERR_CODE_SYS,"罚款有bug");
            }
        }else {
            return Result.fail(Result.ERR_CODE_SYS,Constants.ERROR_MESSAGE);
        }
    }

    @PostMapping("/reserve_book")
    public Result reserveBook(@RequestBody ReserveBookDto reserveBookDto,@RequestHeader("Token") String token){
        Date date = new Date();
        Timestamp now_time = new Timestamp(date.getTime());
        String readerId = TokenUtils.getUserInfo(token,commonService).getUserId();
        boolean isReserve = reserveService.reserveBook(readerId,now_time,reserveBookDto);
        if(isReserve){
            return Result.success("预约成功,预约的书籍为"+reserveBookDto.getBook_id());
        }else {
            return Result.fail(Result.ERR_CODE_SYS,Constants.ERROR_MESSAGE);
        }

    }

    @PostMapping("/get_reserve_list")
    public Result getReserveList(@RequestBody ReserveListDto reserveListDto,@RequestHeader("Token") String token){
        try {
            reserveListDto.setReader_id(TokenUtils.getUserInfo(token,commonService).getUserId());
            Map<String , Object> page = reserveService.getReserveList(reserveListDto);
            return Result.success("查询成功",page);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(Result.ERR_CODE_SYS,Constants.ERROR_MESSAGE);
        }
    }

    @PostMapping("/cancel_reserve")
    public Result cancelReserveById(@RequestBody CancelReserveDto cancelReserveDto){
        boolean isCancel = reserveService.cancelReserveById(cancelReserveDto);
        if(isCancel){
            return Result.success("删除成功");
        }else {
            return Result.fail(Result.ERR_CODE_SYS,Constants.ERROR_MESSAGE);
        }
    }

    @PostMapping("/get_penalty_list")
    public Result getPenaltyList(@RequestBody BorrowHisDto borrowHisDto,@RequestHeader("Token") String token){
       borrowHisDto.setReader_id(TokenUtils.getUserInfo(token,commonService).getUserId());
        try {
            Map<String ,Object> page = reserveService.getPenaltyList(borrowHisDto);
            return Result.success("查询成功",page);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(Result.ERR_CODE_SYS,Constants.ERROR_MESSAGE);
        }

    }


    @GetMapping("/get_all_label_class")
    public Result getAllLabel(){
        try {
            LabelAndClass list = newService.getLabelAndClass();
            return Result.success("成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(Result.ERR_CODE_SYS,Constants.ERROR_MESSAGE);
        }
    }




}
